﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class AdClick
    {
        private readonly KeRui.DAL.AdClick dal = new KeRui.DAL.AdClick();

        public int Add(KeRui.Model.AdClick model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.AdClick> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.AdClick> list = new List<KeRui.Model.AdClick>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.AdClick item = new KeRui.Model.AdClick();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["AdID"].ToString() != "")
                    {
                        item.AdID = int.Parse(dt.Rows[i]["AdID"].ToString());
                    }
                    if (dt.Rows[i]["SellerADID"].ToString() != "")
                    {
                        item.SellerADID = int.Parse(dt.Rows[i]["SellerADID"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public int GetCount(int Type, int AdID)
        {
            string fldName = "";
            string str2 = "";
            if (Type == 1)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(10),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(10),CreateTime,120),AdID";
            }
            else if (Type == 2)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120)";
                str2 = " Group by CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120),AdID";
            }
            else if (Type == 3)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(7),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(7),CreateTime,120),AdID";
            }
            else if (Type == 4)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(4),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(4),CreateTime,120),AdID";
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("AdClick", -1, -1, string.Concat(new object[] { " AdID=", AdID, str2, "" }), " AdID desc", fldName).Tables[0].Rows.Count;
        }

        public int GetCount(int Type, int AdID, string TDate)
        {
            string strWhere = " Adid=" + AdID;
            if (Type == 1)
            {
                strWhere = strWhere + " AND  CONVERT(VARCHAR(10),CreateTime,120) = '" + TDate + "'";
            }
            else if (Type == 2)
            {
                strWhere = strWhere + "AND  CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120) = '" + TDate + "'";
            }
            else if (Type == 3)
            {
                strWhere = strWhere + "AND CONVERT(VARCHAR(7),CreateTime,120) = '" + TDate + "'";
            }
            else if (Type == 4)
            {
                strWhere = strWhere + "AND CONVERT(VARCHAR(4),CreateTime,120) = '" + TDate + "'";
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("AdClick", strWhere);
        }

        public DataSet GetList(int PageSize, int PageIndex, int Type, int AdID)
        {
            string fldName = "";
            string str2 = "";
            if (Type == 1)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(10),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(10),CreateTime,120),AdID";
            }
            else if (Type == 2)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120)";
                str2 = " Group by CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120),AdID";
            }
            else if (Type == 3)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(7),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(7),CreateTime,120),AdID";
            }
            else if (Type == 4)
            {
                fldName = "AdID, CountNum=Count(1), TDate=CONVERT(VARCHAR(4),CreateTime,120)";
                str2 = " Group by CONVERT(VARCHAR(4),CreateTime,120),AdID";
            }
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("AdClick", PageSize, PageIndex, string.Concat(new object[] { " AdID=", AdID, str2, "" }), " TDate desc", fldName);
        }

        public List<KeRui.Model.AdClick> GetList(int PageSize, int PageIndex, int Type, int AdID, string TDate)
        {
            string strWhere = " AdID=" + AdID;
            if (Type == 1)
            {
                strWhere = strWhere + " AND  CONVERT(VARCHAR(10),CreateTime,120) = '" + TDate + "'";
            }
            else if (Type == 2)
            {
                strWhere = strWhere + " AND  CONVERT(VARCHAR(10),DATEADD(wk,DATEDIFF(wk,0,CreateTime),0),120) = '" + TDate + "'";
            }
            else if (Type == 3)
            {
                strWhere = strWhere + " AND  CONVERT(VARCHAR(7),CreateTime,120) = '" + TDate + "'";
            }
            else if (Type == 4)
            {
                strWhere = strWhere + " AND CONVERT(VARCHAR(4),CreateTime,120) = '" + TDate + "'";
            }
            DataSet set = new KeRui.DAL.Common().GetList("AdClick", PageSize, PageIndex, strWhere, " CreateTime desc");
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.AdClick GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.AdClick GetModelByCache(int ID)
        {
            string cacheKey = "AdClickModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.AdClick) cache;
        }
    }
}

